#include "dunLexer.h"

t_lex	lexerCreate(void)
{
	t_lex result = malloc(sizeof (struct s_lex));
	result->num_tokens = 0;
	result->current_token = NULL;
	result->token_q = queueNew();
	return (result);
}

void	lexerSplit(t_lex lex, char* s, char* delim)
{
	int i, j;
	char isSplit;
	free(lex->token_q);
	lex->token_q = queueNew();
	lex->token_q = queuePush(0, lex->token_q);
	for (i = 0; s[i]; i++)
	{
		isSplit = 0;
		for(j = 0; (delim[j] && !isSplit); j++)
		{
			isSplit = isSplit || (s[i] == delim[j]);
		}
		if (isSplit)
		{
			lex->tokens[i] = '\0';
			lex->token_q = queuePush(i+1, lex->token_q);
		}
		else
			lex->tokens[i] = s[i];
	}
	lex->current_token = lex->tokens;
}

char* 	lexerGetToken(t_lex lex)
{
	int i;
	char* result = NULL;
	if (!queueIsEmpty(lex->token_q))
	{
		i = queuePop(&lex->token_q);
		result = &lex->tokens[i];
	}
	return (result);
}

int		lexerGetTokenAsInt(t_lex lex)
{
	char* res = lexerGetToken(lex);
	int result = strtol(res, NULL, 10);
	return (result);
}